package com.shangyueshang.security;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.shangyueshang.enums.UserStatus;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/6 18:40
 * @version: v1.0.0
 * <p>
 * 安全用户模型
 */
public class JwtUserDetails implements UserDetails {
	
	@Setter
	private String username;
	
	@Setter
	private String password;
	
	@Getter
	@Setter
	private String salt;
	
	/**
	 * 状态，0禁用 1启用
	 */
	private UserStatus status;
	
	@Setter
	private Collection<? extends GrantedAuthority> authorities;
	
	/**
	 * 授权店铺
	 */
	@Getter
	private List<String> authorizedShops;
	
	public JwtUserDetails(
			String username,
			String password,
			String salt,
			UserStatus status,
			Collection<? extends GrantedAuthority> authorities,
			List<String> authorizedShops
	) {
		this.username = username;
		this.password = password;
		this.salt = salt;
		this.status = status;
		this.authorities = authorities;
		this.authorizedShops = authorizedShops;
	}
	
	@Override
	public String getUsername() {
		return username;
	}
	
	@JsonIgnore
	@Override
	public String getPassword() {
		return password;
	}
	
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}
	
	@JsonIgnore
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}
	
	@JsonIgnore
	@Override
	public boolean isAccountNonLocked() {
		return true;
	}
	
	@JsonIgnore
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}
	
	@JsonIgnore
	@Override
	public boolean isEnabled() {
		return status.equals(UserStatus.enabled);
	}
}
